#!/bin/bash

INTERVAL=120
HZ=$(getconf CLK_TCK)
result1=$(cat /proc/stat|grep "cpu "|awk '{print $4}')
result3=$(cat /proc/stat|grep "cpu "|awk '{print $2}')
result5=$(cat /proc/stat|grep "cpu "|awk '{print $6}')
result7=$(cat /proc/stat|grep "cpu "|awk '{print $8+$7}')
result9=$(cat /proc/stat|grep "cpu "|awk '{print $11+$9+$10}')
result11=$(cat /proc/stat|grep "cpu "|awk '{print $5}')

sleep ${INTERVAL}s

result2=$(cat /proc/stat|grep "cpu "|awk '{print $4}')
result4=$(cat /proc/stat|grep "cpu "|awk '{print $2}')
result6=$(cat /proc/stat|grep "cpu "|awk '{print $6}')
result8=$(cat /proc/stat|grep "cpu "|awk '{print $8+$7}')
result10=$(cat /proc/stat|grep "cpu "|awk '{print $11+$9+$10}')
result12=$(cat /proc/stat|grep "cpu "|awk '{print $5}')

now=$(date '+%Y-%m-%d %H:%M:%S')

cpu_usage_system=$(echo "scale=4; ($result2 - $result1)/$HZ/$INTERVAL*100" | bc)
cpu_usage_user=$(echo "scale=4; ($result4 - $result3)/$HZ/$INTERVAL*100" | bc)
cpu_usage_iowait=$(echo "scale=4; ($result6 - $result5)/$HZ/$INTERVAL*100" | bc)
cpu_usage_IRQS=$(echo "scale=4; ($result8 - $result7)/$HZ/$INTERVAL*100" | bc)
cpu_usage_others=$(echo "scale=4; ($result10 - $result9)/$HZ/$INTERVAL*100" | bc)
cpu_usage_idle=$(echo "scale=4; ($result12 - $result11)/$HZ/$INTERVAL*100" | bc)

echo $now

echo "CPU Busy System: $cpu_usage_system "
echo "CPU Busy User: $cpu_usage_user "
echo "CPU Busy Iowait: $cpu_usage_iowait "
echo "CPU Busy IRQS: $cpu_usage_IRQS "
echo "CPU Busy Others: $cpu_usage_others "
echo "CPU Busy Idle: $cpu_usage_idle "

